//
//  DHUserAccount.m
//  精品酒店
//
//  Created by DIANGHONG on 2018/5/8.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "DHUserAccount.h"
#define DHUserAccountJson @"userAccount.json"

@implementation DHUserAccount
- (NSString *)description
{
   return [self yy_modelDescription];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        //取出json数据
        NSString *docPath = [DHUserAccountJson dh_appendDocumentDir];
        NSData *data = [NSData dataWithContentsOfFile:docPath];
        NSLog(@"%@",docPath);
        
        if (!data) {
            return self;
        }
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        //字典转模型
        [self yy_modelSetWithJSON:dict];
    }
    return self;
}
/**
 保存用户模型到沙河
 */
- (void)saveAccount{
    
    //获取doc路径
    NSString *docPath = [DHUserAccountJson dh_appendDocumentDir];
    
    //模型转字典
    NSMutableDictionary *dict = [self yy_modelToJSONObject];
    
    //json反序列化
    NSData *data = [dict yy_modelToJSONData];
    
    //写入磁盘
    [data writeToFile:docPath atomically:true] ? nil : NSLog(@"写入失败");
}
- (void)removeAccount{
    //获取doc路径
    NSString *docPath = [DHUserAccountJson dh_appendDocumentDir];
    
    //移除用户
    [NSFileManager.defaultManager removeItemAtPath:docPath error:nil];
}
@end
